/*
 * @lc app=leetcode.cn id=2515 lang=cpp
 *
 * [2515] 到目标字符串的最短距离
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
    int n;
    int next(int i)
    {
        return (i + 1) % n;
    }
    int pre(int i)
    {
        return (i - 1 + n) % n;
    }

public:
    int closetTarget(vector<string> &words, string target, int startIndex)
    {
        if (words[startIndex] == target)
            return 0;

        n = words.size();
        int l = pre(startIndex);
        int r = next(startIndex);

        int ans = 1;
        while (l != r)
        {
            if (words[l] == target || words[r] == target)
                return ans;
            ans++;
            l = pre(l);
            r = next(r);
        }
        if (words[l] == target || words[r] == target)
            return ans;
        return -1;
    }
};
// @lc code=end
